package com.miao.teacher.ui;

import java.math.BigDecimal;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.espeaker.sdk.Constants;
import com.espeaker.sdk.api.ReviseApi;
import com.espeaker.sdk.utils.JsonUtils;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.miao.teacher.adapter.FeedbackAdapter;
import com.miao.teacher.adapter.OrderAdapter;
import com.miao.teacher.app.TApp;
import com.miao.teacher.models.FeedBack;
import com.miao.teacher.models.PullOrder;
import com.miao.teacher.utils.FileUtil;

import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class FeedbackFragment  extends BaseFragment {
	
	ReviseApi api=new ReviseApi();
	private PullToRefreshListView lv = null;
	private FeedbackAdapter mAdapter = null;
	
	FeedBack f=new FeedBack();
	
	TextView tvCount;
	TextView tvAvage;
	/*
	 * 创建一个实例
	 */
	public static FeedbackFragment newInstance() {
		FeedbackFragment fragment = new FeedbackFragment();
		return fragment;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.layout_feedback, null);
		lv = (PullToRefreshListView) view.findViewById(R.id.lvPull);
		tvCount=(TextView)view.findViewById(R.id.tv_title);
		tvAvage=(TextView)view.findViewById(R.id.tv_rate);
		lv.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				// TODO Auto-generated method stub
				String userId = FileUtil.getPreferences(getActivity(), TApp.USER_ID);
				api.feedback(userId, mHandler);
			}
		});
		return view;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);	
	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		String userId = FileUtil.getPreferences(getActivity(), TApp.USER_ID);
		api.feedback(userId, mHandler);
	}
	@Override
	protected void RecMsgFromHandler(Message msg) {
		// TODO Auto-generated method stub
		super.RecMsgFromHandler(msg);
		switch (msg.what) {
		case Constants.REVISE_FEEDBACK:
			showFeedBack(msg.obj);
			break;
		}
	}
	public double Number1(double pDouble)
	{
	  BigDecimal  bd=new  BigDecimal(pDouble);
	  BigDecimal  bd1=bd.setScale(1,bd.ROUND_HALF_UP);
	  pDouble=bd1.doubleValue();
	  long  ll = Double.doubleToLongBits(pDouble);
	  return pDouble;
	}
	public void showFeedBack(Object obj)
	{
		try {
			final JSONObject json = new JSONObject(obj.toString());
			int status = JsonUtils.getInt(json, "status", -1);
			if (status == 0) {
				JSONObject valueJson = JsonUtils.getJSONObject(json, "values",
						null);
				JSONArray jArray=valueJson.getJSONArray("feedback");
				mAdapter = new FeedbackAdapter(getActivity().getBaseContext(),
						f.parse(jArray));
				try{
				lv.setAdapter(mAdapter);
				}catch(Exception er)
				{}
				tvCount.setText(tvCount.getText().toString().replace("{0}", ""+mAdapter.getCount()));
				Double d=new Double(f.total_rate/mAdapter.getCount());
				tvAvage.setText(Number1(d)+"");
			}
			lv.onRefreshComplete();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
